﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib ;


namespace DVSProject.Controllers
{
    public class FeederRemarkController : Controller
    {
        //
        // GET: /FeederRemark/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
            IEnumerable<FeederRemarkModels> _FeederRemarks = _FeederRemarkRepo.GetFeederRemark();
            return View(_FeederRemarks);
        }
        //
        // GET: /FeederRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
            FeederRemarkModels _FeederRemark = new FeederRemarkModels();
            _FeederRemark = _FeederRemarkRepo.GetFeederRemarkById(id);
            return View(_FeederRemark);
        }

        //
        // GET: /FeederRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            FeederRemarkModels _FeederRemark = new FeederRemarkModels();

            TitleFeeDerRepo _TitleFeeDerRepo = new TitleFeeDerRepo();
            IEnumerable<TitleFeeDerModels> _TitleOnsiteTranings = _TitleFeeDerRepo.GetTitleFeeDer();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleOnsiteTranings
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleFeeDerId == selectedId),
                                                         Text = s.TitleFeeDerName,
                                                         Value = s.TitleFeeDerId.ToString()
                                                     };

            _FeederRemark.TitleSelect = selectlist;
            return View(_FeederRemark);
        } 

        //
        // POST: /FeederRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
                FeederRemarkModels _FeederRemark = new FeederRemarkModels();
                _FeederRemark.TitleFeeDerId = SF.GetInt32(collection["TitleFeeDerId"]);
                _FeederRemark.FeederRemarkTh = collection["FeederRemarkTh"];
                _FeederRemark.FeederRemarkEng = collection["FeederRemarkEng"];
                _FeederRemark.CreateBy = (string)Session["User"];

                bool result = _FeederRemarkRepo.Create(_FeederRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /FeederRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
            FeederRemarkModels _FeederRemark = new FeederRemarkModels();
            _FeederRemark = _FeederRemarkRepo.GetFeederRemarkById(id);

            TitleFeeDerRepo _TitleFeeDerRepo = new TitleFeeDerRepo();
            IEnumerable<TitleFeeDerModels> _TitleOnsiteTranings = _TitleFeeDerRepo.GetTitleFeeDer();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleOnsiteTranings
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleFeeDerId == selectedId),
                                                         Text = s.TitleFeeDerName,
                                                         Value = s.TitleFeeDerId.ToString()
                                                     };

            _FeederRemark.TitleSelect = selectlist;
            return View(_FeederRemark);
        }

        //
        // POST: /FeederRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
                FeederRemarkModels _FeederRemark = new FeederRemarkModels();
                _FeederRemark.Id = id;
                _FeederRemark.TitleFeeDerId = SF.GetInt32(collection["TitleFeeDerId"]);
               // _FeederRemark.FeederRemarkTh = collection["FeederRemarkTh"];
                _FeederRemark.FeederRemarkTh = collection["FeederRemarkTh"];
                _FeederRemark.FeederRemarkEng = collection["FeederRemarkEng"];
                _FeederRemark.UpdateBy = (string)Session["User"];
                bool result = _FeederRemarkRepo.Edit(_FeederRemark);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /FeederRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
            FeederRemarkModels _FeederRemark = new FeederRemarkModels();
            _FeederRemark = _FeederRemarkRepo.GetFeederRemarkById(id);
            return View(_FeederRemark);
        }

        //
        // POST: /FeederRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                FeederRemarkRepo _FeederRemarkRepo = new FeederRemarkRepo();
                FeederRemarkModels _FeederRemark = new FeederRemarkModels();
                _FeederRemark.Id = id;
                _FeederRemark.UpdateBy = (string)Session["User"];
                bool result = _FeederRemarkRepo.Delete(_FeederRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
